-- displays data gathered by the core in FuBar
local name = ...
local WhoRoll = WhoRoll
local debug = WhoRoll.debug

local L = LibStub("AceLocale-3.0"):GetLocale(name)

local tooltip = LibStub('LibQTip-1.0'):Acquire('WhoRollTooltip', 3)

-- globals
local _G = _G
local NEED = _G.NEED
local GREED = _G.GREED
local PASS = _G.PASS
local DISENCHANT = "DISENCHANT"
local GetItemInfo = _G.GetItemInfo
local GetItemQualityColor = _G.GetItemQualityColor
local UnitInRaid = _G.UnitInRaid
local UnitInParty = _G.UnitInParty
local GetNumPartyMembers = _G.GetNumPartyMembers
local GetNumRaidMembers = _G.GetNumRaidMembers
local UnitName = _G.UnitName

local lootIcons = {
	NEED = "Interface/Buttons/UI-GroupLoot-Dice-Up",
	GREED = "Interface/Buttons/UI-GroupLoot-Coin-Up",
	PASS = "Interface/Buttons/UI-GroupLoot-Pass-Up",
	DISENCHANT = "Interface/Buttons/UI-GroupLoot-DE-Up",
}

local ICON_PATTERN = "|T%s:0:0:0:0|t"
local function getIconString(choice)
	if not choice then
		return ""
	else
		local icon = lootIcons[choice]
		if not icon then return "" end -- or 1.0.0-savedvars
		return ICON_PATTERN:format(icon)
	end
end

local function addItemLine(itemLink)
	local itemName, _, quality, _, _, _, _,_,_, texture = GetItemInfo(itemLink)
	local itemString
	if not itemName or not quality or not texture then
		itemString = itemLink
		texture = "Interface/ICONS/INV_Misc_QuestionMark"
	else
		local _,_,_,hex = GetItemQualityColor(quality)
		itemString = ("%s%s|r"):format(hex, itemName or UNKNOWN)
	end

	local textureString = ICON_PATTERN:format(texture)
	local line = tooltip:AddLine(textureString)
	tooltip:SetCell(line, 2, itemString, nil, "LEFT", 2)
end

local groupmembers = {}
local function scanGroupMembers()
	wipe(groupmembers)
	if UnitInRaid("player") then
		for i = 1,GetNumRaidMembers() do
			local name = UnitName("raid" .. i)
			table.insert(groupmembers, name)
		end
	elseif UnitInParty("player") then
		groupmembers[1] = UnitName("player")
		for i = 1,GetNumPartyMembers() do
			local name = UnitName("party" .. i)
			table.insert(groupmembers, name)
		end
	end
end

--[===[@debug@
function WhoRoll:DebugBroker()
	scanGroupMembers = function() end
	groupmembers = { UnitName("player"), "test1", "test2", "asdf", "hey42", }
end
--@end-debug@]===]

local function updateTooltip()
	tooltip:Clear()
	local title = tooltip:AddHeader()
	tooltip:SetCell(title, 1, "WhoRoll", tooltip:GetHeaderFont(), "LEFT", 3)
	local count = 0
	for k,v in pairs(WhoRoll.currentLoots) do
		addItemLine(k)
		for name in WhoRoll:IterateGroupMembers() do
			tooltip:AddLine(nil, getIconString(v.rolls[name]), name)
		end
		count = count + 1
	end
	tooltip:AddSeparator()
	for _,v in pairs(WhoRoll.rollHistory) do
		addItemLine(v.item)
		if v.winner then
			local dateString = date(WhoRoll.db.profile.dateformat, v.date)
			tooltip:AddLine(nil, getIconString(v.choice), ("%s (%s)"):format(v.winner, dateString))
		end
	end
	tooltip:AddSeparator()
	local foot = tooltip:AddLine()
	tooltip:SetCell(foot, 1, L["|c0000cc00Shift+Click|r to clear the history"], nil, "LEFT", 3)
	foot = tooltip:AddLine()
	tooltip:SetCell(foot, 1, L["|c0000cc00RightClick|r for options"], nil, "LEFT", 3)
	return count
end

local function countCurrentLoots()
	local count = 0
	for k,v in pairs(WhoRoll.currentLoots) do
		count = count + 1
	end
	return count
end

local doUpdate = countCurrentLoots
function WhoRoll:UpdateTooltip()
	local count = doUpdate()
	if count > 0 then
		count = "|cff1eff00" .. count .. "|r"
	end
	self.obj.text = string.format(L["%s Rolls"], count)
end

local ldbdata = {
	type = "data source",
	icon = [[Interface/Buttons/UI-GroupLoot-Dice-Up]],
	OnClick = function(_, msg)
			if msg == "LeftButton" then
				if IsShiftKeyDown() then
					wipe(WhoRoll.rollHistory)
				end
				updateTooltip()
			elseif msg == "RightButton" then
				WhoRoll:ShowConfig()
			end
		end,
	label = "WhoRoll",
	OnEnter = function (self)
		doUpdate = updateTooltip
		doUpdate()
		tooltip:SmartAnchorTo(self)
		tooltip:Show()
	end,
	OnLeave = function (self)
		doUpdate = countCurrentLoots
		tooltip:Hide()
	end,
}

WhoRoll.obj = LibStub("LibDataBroker-1.1"):NewDataObject("WhoRoll", ldbdata)
